#include "StdAfx.h"

#using <mscorlib.dll>
#include <vcclr.h>
#include <string.h>

using namespace System;

public struct UirCfg
{
  UInt32 unknown1;
  UInt32 unknown2;
  UInt32 region;
  UInt32 device;       
  UInt32 vendor;
  UInt32 codeset;
  UInt32 unknown3;
  UInt32 unknown4;
  UInt32 minimumDigits;
  UInt32 digitDelay;
  UInt32 needEnter;
  UInt32 enterDelay;
  UInt32 tuneDelay;
  UInt32 oneDigitDelay;
};

UInt16 __stdcall UIR_Open(UInt32 verbose, UInt16 i2cAddress)
{
  return 1;
}

UInt32 __stdcall UIR_Close()
{
  return 0;
}

UInt32 __stdcall UIR_GetConfig(UInt32 device, UInt32 codeset, UirCfg* pCfg)
{
  memset(pCfg, 0, sizeof(*pCfg));
  return 0;
}

UInt32 __stdcall UIR_GotoChannel(UInt32 device, UInt32 codeset, UInt32 channel)
{
  try {
    // Get the location of this DLL. This is the location of our configuration file
    String^ dllLocation = System::Reflection::Assembly::GetExecutingAssembly()->Location;

    // Create an instance of the DTVSerialPortController
    HDPVRIrBlasterBridge::DirecTVSerialBlaster^ managedDirecTVSerialBlaster = gcnew HDPVRIrBlasterBridge::DirecTVSerialBlaster(dllLocation);
    try {
      // Change the channel on the controller.
      return managedDirecTVSerialBlaster->SetChannel(channel, true) ? 0 : 1;
    }
    finally {
      // This will call "Dispose()" on the managed class so we can release the resources allocated by the serial port.
      delete managedDirecTVSerialBlaster;
    }
  }
  catch (Exception^ ex) 
  {
    // For some reason we threw an exception. Log the reason in the event log and report the failure to the DvbsBridgeSvc as a failure.
    System::Diagnostics::EventLog::WriteEntry("HDPVRIrBlasterBridge", String::Format("ERROR: UIR_GotoChannel: {0}", ex->Message), System::Diagnostics::EventLogEntryType::Error);
    return 1;
  }
  return 0;
}

